<?php

require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT . 'controller/form.inc.php';
require_once WACT_ROOT . 'validation/rules/match.inc.php';
require_once WACT_ROOT . 'validation/rules/member.inc.php';
require_once WACT_ROOT . 'validation/rules/pattern.inc.php';
require_once APP_ROOT . 'model/common/page.php';
require_once APP_ROOT . 'model/common/leftmenu.php';
require_once APP_ROOT . 'model/common/categories.php';
require_once APP_ROOT . 'model/common/session.php';
require_once APP_ROOT . 'model/common/form.php';


// create/update form
class PageFormController extends FormController {

	var $admin_user = false;
	var $staff_user = false;
	var $view; // current view

	function setUserType($admin_user=false, $staff_user = false) {
		$this->admin_user = $admin_user;
        $this->staff_user = $staff_user;
	}

    function setView(&$view) {
    	$this->view = $view;
    }


	 function PageFormController() {
     	$this->addChild('create', new ButtonController(new Delegate($this, 'create'))); // create
        $this->addChild('save', new ButtonController(new Delegate($this, 'save'))); // save
        $this->addChild('delete', new ButtonController(new Delegate($this, 'delete'))); // delete

        $this->addChild('clean', new ButtonController(new Delegate($this, 'clean'))); // clean
        $this->addChild('pages', new ButtonController(new Delegate($this, 'back'))); // clean

        $this->setDefaultChild('clean');

        // validation
		$this->addRule(new Handle('RequiredRule', array('headline')));
        $this->addRule(new Handle('SizeRangeRule', array('headline', 50)));


        $this->registerOnLoadListener(new Delegate($this, 'load'));
        //$this->registerOnActivateListener(new Delegate($this, 'check'));

        $this->addView('delete', new Handle('RedirectView', array('/delete')));

	 }

    function create(&$source, &$request, &$responseModel) {
     	if ($responseModel->isValid()) {
            require_once WACT_ROOT.'/util/phpcompat/clone.php';

            //prepare author user
            $author_user_id = 0;
            if (Session::isLogin()) {
            	$author_user_id = Session::getUserId();
            }
            if ($author_user_id) $responseModel->set('user_id',$author_user_id);

            $show_menu = $responseModel->get('show_menu');
            if (!$show_menu) $responseModel->set('parent_menu_id',0);

            $responseModel->set('date_added', date("Y-m-d H:i") );
            $page_id = Page::insertPage($responseModel);
            if ($page_id) {
            	$responseModel->set('page_id', $page_id);
                $lastRecord = clone_obj($responseModel);


                // Set up the model for adding the next record
                $responseModel->removeAll();
                $responseModel->set('LastRecord', $lastRecord);


                return 'createOK';
            } else {
                return 'failure';
            }
        } else {

        }
     }


	 function delete(&$source, &$request, &$responseModel) {
	 	return 'delete';
	 }


     function save(&$source, &$request, &$responseModel) {
        if ($responseModel->isValid()) {
        	$page_id = $request->getParameter('page_id');

            //prepare author user
            $admin_user_id = 0;
            if (Session::isLogin()) {
            	$admin_user_id = Session::getUserId();
            }
            if ($admin_user_id) $responseModel->set('user_id',$admin_user_id);

            $show_menu = $responseModel->get('show_menu');
            if (!$show_menu) $responseModel->set('parent_menu_id',0);

            if ($page_id) Page::updatePage($page_id, $responseModel);
            return 'saveOK';
        }
    }

    function clean(&$source, &$request, &$responseModel) {
        $responseModel->removeAll();
        $responseModel->set('supress_errors', 1);
    	return 'cleanOK';
    }

    function back(&$source, &$request, &$responseModel) {
        $responseModel->removeAll();
        $responseModel->set('supress_errors', 1);
    	return 'index';
    }

    function load(&$source, &$request, &$responseModel) {
     	$page_id = $request->getParameter('page_id');

     	if ($page_id) {
        	$record =& Page::getRecord($page_id);
        	if (is_null($record)) {
            	return 'notfound';
        	}
            $responseModel->import($record->export());

             // populate public_url
            $public_url = Page::getPublicURLByID($page_id);
            $responseModel->set('public_url', $public_url);

            // prepare the author
            $author_show = User::getFullUserName($responseModel);
            $responseModel->set('author_show', $author_show);


        }
     }

     //prepare the form view
     function prepare(&$source, &$request, &$responseModel, &$template) {
     	//print 'Prepare form...';
     	if ($this->admin_user) {
     		$responseModel->set('admin_user',1);
     	}
     	if ($this->staff_user) $responseModel->set('staff_user',1);

        //populate types
        $types_list =& Page::getTypesList();
        $types =& $template->getChild('type');
        $types->setChoices($types_list);

        //populate menus
        $menus_list =& LeftMenuModel::getRootMenusForCombo(1);
        $menus =& $template->getChild('parent_menu_id');
        $menus->setChoices($menus_list);





     }



}

?>